package com.whxy02.tolearn.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whxy02.tolearn.app.beans.Question;
import com.whxy02.tolearn.app.beans.QuestionType;
import com.whxy02.tolearn.app.dao.QuestionDao;
import com.whxy02.tolearn.app.resultObject.QuestionResultType;
import com.whxy02.tolearn.app.service.QuestionService;
import com.whxy02.tolearn.app.service.QuestionTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    private QueryWrapper<Question> wrapper=new QueryWrapper<>();
    @Resource
    QuestionDao questionDao;

    @Resource
    QuestionTypeService qts;

    @Override
    public List<Question> getAll() throws Exception {
        return questionDao.selectList(null);
    }

    @Override
    public Question getById(Integer id) throws Exception {
        return questionDao.selectById(id);
    }

    @Override
    public int save(Question question) throws Exception {
        return questionDao.insert(question);
    }

    @Override
    public int removeById(Integer id) throws Exception {
        return questionDao.deleteById(id);
    }

    @Override
    public int change(Question question) throws Exception {
       return questionDao.updateById(question);
    }

    @Override
    public List<Question> getByLimit(Integer subject_id, QuestionResultType result) throws Exception {
        List<QuestionType> questionTypes=qts.getBySubject(subject_id);
        List<Question> questions=new ArrayList<>();

        for(QuestionType questionType:questionTypes){
            List<Question> question=questionDao.selectByRand(subject_id,questionType.getQuestionTypeId());
            for(Question q:question){
                if(questionType.getTypeName().equals("单选题"))
                    q.setDisplay(1);
                else if(questionType.getTypeName().equals("多选题")){
                    q.setDisplay(2);
                }else if(questionType.getTypeName().equals("填空题")){
                    q.setDisplay(3);
                }
            }
            questions.addAll(question);
            result.addQuestionInfos(questionType.getTypeName(),question.size());

        }

        return questions;
    }

    @Override
    public List<Question> getByLimit(Integer subject_id,Integer tags_id, QuestionResultType result) throws Exception {
        List<QuestionType> questionTypes=qts.getBySubject(subject_id);
        List<Question> questions=new ArrayList<>();

        for(QuestionType questionType:questionTypes){
            List<Question> question=questionDao.selectWithTagsRand(subject_id,questionType.getQuestionTypeId(),tags_id);
            for(Question q:question){
                if(questionType.getTypeName().equals("单选题"))
                    q.setDisplay(1);
                else if(questionType.getTypeName().equals("多选题")){
                    q.setDisplay(2);
                }else if(questionType.getTypeName().equals("填空题")){
                    q.setDisplay(3);
                }
            }
            questions.addAll(question);
            result.addQuestionInfos(questionType.getTypeName(),question.size());
        }
        return questions;
    }

    @Override
    public Page<Question> getByPage(Integer nowPage, Integer subjectId, Integer tagsId) throws Exception {
        wrapper.clear();
        Page<Question> page=new Page<>(nowPage,10);

        wrapper.eq("subject_id",subjectId).eq("tags_id",tagsId);
        return questionDao.selectPage(page,wrapper);
    }

    @Override
    public Page<Question> getByPage(Integer nowPage,Integer subjectId) throws Exception {
        wrapper.clear();
        Page<Question> page=new Page<>(nowPage,10);

        wrapper.eq("subject_id",subjectId);
        return questionDao.selectPage(page,wrapper);
    }

    @Override
    public List<Question> getByIds(List<Integer> ids) throws Exception {
        wrapper.clear();
        wrapper.in("question_id",ids);
        return questionDao.selectList(wrapper);
    }

}
